package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nuc.modle.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyImageAndTextListAdapter extends ArrayAdapter<WeiBoData> {

	public MyImageAndTextListAdapter(Context context,
			List<WeiBoData> newsList) {
		super(context, 0, newsList);
	}

	private AsyncImageLoader imageLoader = new AsyncImageLoader();

	private Map<Integer, View> viewMap = new HashMap<Integer, View>();

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = this.viewMap.get(position);

		if (rowView == null) {
			LayoutInflater inflater = ((Activity) this.getContext())
					.getLayoutInflater();
			rowView = inflater.inflate(R.layout.mainitem, null);
			WeiBoData weiBoData = this.getItem(position);

			TextView textView = (TextView) rowView.findViewById(R.id.mainNickId);
			textView.setText(weiBoData.getName());

			final ImageView imageView = (ImageView) rowView
					.findViewById(R.id.picId);
			Drawable imageDrawable = null;
			if(weiBoData.getImage() != null){
				imageDrawable = imageLoader.loadDrawable(weiBoData.getImage().get(0) + "/460", new AsyncImageLoader.ImageCallback() {

					public void imageLoaded(Drawable imageDrawable) {
						imageView.setImageDrawable(imageDrawable);
					}
				});
				if(imageDrawable != null){
					imageView.setImageDrawable(imageDrawable);
				}
			}
			
			final ImageView headImageView = (ImageView)rowView.findViewById(R.id.headImageId);
			Drawable headDrawable = null;
			headDrawable = imageLoader.loadDrawable(weiBoData.getHead() + "/50", new AsyncImageLoader.ImageCallback() {
				
				@Override
				public void imageLoaded(Drawable imageDrawable) {
					headImageView.setImageDrawable(imageDrawable);
				}
			});
			if(headDrawable != null){
				headImageView.setImageDrawable(headDrawable);
			}
			viewMap.put(position, rowView);
		}   
		return rowView;
	}

}
